# event listener
from direct.showbase import DirectObject
# event
from EventDefine import event_QNETWORK_SENDDIRECTLY
from QCmdInputPipe import QCmdInputPipe
# IDMapping
from Cube.utility.IDA2IDBMap import IDA2IDBMap

class QNetworkPipe(QCmdInputPipe, DirectObject.DirectObject):
    def __init__(self, CmdDispatchPeriods = 0.1):
        QCmdInputPipe.__init__(self, CmdDispatchPeriods, SyncMode = True)
        self.CmdDispatchEnable = False
        self._NetworkObj = None  # override it
        self.MemberID2LinkIDMap = IDA2IDBMap()

        self.accept(event_QNETWORK_SENDDIRECTLY, self.CommandSendDirectly)

    def MemberID2LinkIDMapGet(self):
        ret = self.MemberID2LinkIDMap
        return ret

    def CommandSendDirectly(self, dataObject, LinkID):
        if LinkID is None:
            self._NetworkObj.DataObjSendAll(dataObject)
        else:
            self._NetworkObj.DataObjSend(dataObject, targetID = LinkID)

    # override it
    def DisconnectionHandle(self, deadConnectionIndexList):
        pass

    def destroy(self):
        QCmdInputPipe.destroy(self)
